package com.probeez.profiles.gsensor;

import static com.probeez.profiles.gsensor.WindowOrientationListener.TILT_POSITION_THERSHOLD;
import android.app.Activity;
import android.content.ComponentName;
import android.content.Intent;
import android.content.ServiceConnection;
import android.os.Bundle;
import android.os.Handler;
import android.os.IBinder;
import android.os.Message;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.SeekBar;
import android.widget.TextView;
import android.widget.Toast;
import android.widget.SeekBar.OnSeekBarChangeListener;

public class PluginIntroActivity extends Activity implements OnSeekBarChangeListener, OnClickListener {
  private static final int MSG_INIT = 0;

  private class LocalServiceConnection implements ServiceConnection {
			public void onServiceConnected(ComponentName className, IBinder service) {
          mService = ((EvalConditionService.LocalBinder)service).getService();
          Message.obtain(mHandler, MSG_INIT).sendToTarget();
      }
      public void onServiceDisconnected(ComponentName className) {
          mService = null;
      }
  };
	
	private TextView mDelta;
	private SeekBar mSeekbar;
  private EvalConditionService mService;
  private LocalServiceConnection mServiceConn;
  private Handler mHandler = new Handler() {
		@Override
    public void dispatchMessage(Message msg) {
			if (msg.what==MSG_INIT) {
				onInit();
			}
    }
  };

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.intro);
		mDelta = (TextView) findViewById(R.id.delta);
		mSeekbar = (SeekBar) findViewById(R.id.seekbar);
		mSeekbar.setOnSeekBarChangeListener(PluginIntroActivity.this);
		Button button = (Button) findViewById(R.id.button);
		button.setOnClickListener(this);
	}

	private void onInit() {
		View layout = findViewById(R.id.calibrator);
		layout.setVisibility(View.VISIBLE);
		int threshold = mService.getPositionThreshold();
		int progress = (int)((float)threshold/TILT_POSITION_THERSHOLD*100);
		mDelta.setText(String.valueOf(threshold));
		mSeekbar.setProgress(progress);
  }

	@Override
  protected void onResume() {
	  super.onResume();
		mServiceConn = new LocalServiceConnection();
    bindService(new Intent(this, EvalConditionService.class), mServiceConn, BIND_AUTO_CREATE);
  }

	@Override
  protected void onPause() {
	  super.onPause();
	  if (mServiceConn!=null) {
      unbindService(mServiceConn);
      mServiceConn = null;
	  }
  }

	@Override
	public void onProgressChanged(SeekBar seekBar, int progress, boolean fromUser) {
		if (mService!=null && fromUser) { 
			int threshold = (int)((float)progress/100*TILT_POSITION_THERSHOLD);
			mDelta.setText(String.valueOf(threshold));
		}
	}

	@Override
	public void onStartTrackingTouch(SeekBar seekBar) {
	}

	@Override
	public void onStopTrackingTouch(SeekBar seekBar) {
	}

	@Override
  public void onClick(View v) {
		if (mService!=null) {
			int progress = mSeekbar.getProgress();
			int threshold = (int)((float)progress/100*TILT_POSITION_THERSHOLD);
			mService.setPositionThreshold(threshold);
			Toast.makeText(this, getString(R.string.sensor_calibrated), Toast.LENGTH_SHORT).show();
		}
  }

}
